# -*- coding: utf-8 -*-

from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
from urllib.parse import urlencode


class Arrangement(object):

    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "公共交通工具乘客意外险"
    # 生成产品数据
    def genProductInfo(self):
        plan = {
            "planA":{

                "days": ["3d", "1w", "15d", "1m", "3m", "6m", "1y"],
                "people_count": [str(i) for i in range(1, 11)],
                "object": [{
                    "airplane_death":"500000",
                    "train_death":"100000",
                    "ship_disability":"100000",
                    "passenger_death":"20000"
                }]

            },
            "planB":{

                "days": ["3d", "1w", "15d", "1m", "3m", "6m", "1y"],
                "people_count": [str(i) for i in range(1, 11)],
                "object": [{
                    "airplane_death": "2000000",
                    "train_death": "200000",
                    "ship_disability": "200000",
                    "passenger_death": "20000"
                }]

            },
            "planC": {

                "days": ["3d", "1w", "15d", "1m", "3m", "6m", "1y"],
                "people_count":[str(i) for i in range(1,11)],
                "object": [{
                    "airplane_death": "5000000",
                    "train_death": "5000000",
                    "ship_disability": "500000",
                    "passenger_death": "50000"
                }]

            },


        }

        for k in plan:

            for v in product(plan[k]["days"],plan[k]["people_count"],plan[k]["object"]):

                data = {

                    "plan":k,
                    "days":v[0],
                    "people_count":v[1],
                    "object":v[2]
                }
                yield data
    #请求数据
    def requestJson(self):

        for v in self.genProductInfo():

            if v["plan"] == "planA":

                print("/*~~~计划A~~~*/")
                suiteId = "5200"

                numberCode = [str(i) for i in range(26674,26678)]

            elif v["plan"] == "planB":
                print("/*~~~计划B~~~*/")
                numberCode = [str(i) for i in range(26670, 26674)]
                suiteId = "5199"
            else:
                print("/*~~~计划C~~~*/")
                numberCode = [str(i) for i in range(26666, 26670)]
                suiteId = "5198"

            if "d" in v["days"]:

                days = v["days"].replace("d","")

            elif "w" in v["days"]:

                days = "7"
            elif "m" in v["days"]:

                days = str(int(v["days"].replace("m","")) * 30)

            else:

                days = "365"

            data = {

                "insuranceid_"+numberCode[0]+"_保险金额": v["object"]["ship_disability"],
                "insuranceid_"+numberCode[1]+"_保险金额": v["object"]["airplane_death"],
                "insuranceid_"+numberCode[2]+"_保险金额": v["object"]["passenger_death"],
                "insuranceid_"+numberCode[3]+"_保险金额": v["object"]["train_death"],
                "suiteId": suiteId,
                "period": days,
                "amount": suiteId,
                "personCount": v["people_count"],
                "backInsuranceCount":"",
                "minorNumberCount":"",
                "suiteTypeCode": "22",
                "nvhlType": "trafficAccident",
            }

            payload = urlencode(data).replace("+","")

            r = requests.post( url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload)


            if r.status_code == 200:

                price = Helper.decimalParse(r.json()["extremePrice"])

                data = {
                    "name":self.name,
                    "company":"永诚保险",
                    "days":days,
                    "people_count":v["people_count"],
                    "price":price,
                    "airplane_death":v["object"]["airplane_death"],
                    "train_death":v["object"]["train_death"],
                    "ship_disability": v["object"]["ship_disability"],
                    "passenger_death": v["object"]["passenger_death"]
                }
                self.mergeTable( data )



    def mergeTable(self, data):

        SQL = 'INSERT INTO `'+TABLE_ACCIDENT+'`(`name`,`company`,`days`,`people_count`,`price`,`airplane_death`,`train_death`,`ship_disability`,`passenger_death`)VALUES("{name}","{company}","{days}","{people_count}","{price}","{airplane_death}","{train_death}","{ship_disability}","{passenger_death}")'

        print(SQL.format(**data))

        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()


if __name__ == '__main__':

    a = Arrangement()
    a.requestJson()